﻿using System;
using System.Threading;
using System.Collections.Generic;

namespace WebServer.Service.BLL
{
    /// <summary>
    /// 程序域缓冲区
    /// </summary>
    public class WebHostCache
    {
        #region "字段"

        private static WebHostCache instance;

        private Dictionary<String, WebHost> innerCache = new Dictionary<String, WebHost>();

        #endregion

        #region "属性"

        public static WebHostCache Instance
        {
            get
            {
                if (instance != null) return instance;
                WebHostCache tempWebhost = new WebHostCache();
                Interlocked.CompareExchange<WebHostCache>(ref instance, tempWebhost, null);
                return instance;
            }
        }

        #endregion

        #region "公共方法"

        /// <summary>
        /// 根据域名获取程序域
        /// </summary>
        /// <param name="hostName"></param>
        /// <returns></returns>
        public WebHost GetWebHost(String hostName)
        {
            if (innerCache.ContainsKey(hostName))
                return innerCache[hostName];
            return null;
        }

        /// <summary>
        /// 添加程序域到缓冲区中
        /// </summary>
        /// <param name="host"></param>
        /// <param name="hostName"></param>
        public void AddToCache(WebHost host, String hostName)
        {
            if (!innerCache.ContainsKey(hostName))
                innerCache.Add(hostName, host);
        }

        #endregion

        #region "私有方法"

        private WebHostCache() { }

        #endregion
    }
}
